﻿using Excel2DbTool.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Excel2DbTool.Models
{
    /// <summary>
    /// 对应Excel中的每个单元格
    /// </summary>
    public class Cell
    {
        public Cell(String fieldName, Object val)
        {
            this.FieldName = fieldName;
            this.Value = val;
        }

        public String FieldName { get; private set; }


       

        private Object val;
        /// <summary>
        /// Cell's value
        /// </summary>
        public Object Value
        {
            get
            {
                if (DateTimeHelper.IsDateTimeType(val.ToString()))
                {
                    //TODO 空值处理
                    return string.Format("'{0}'", val.ToString());
                    //var dateTime = ((DateTime)(Convert.ChangeType(val, typeof(DateTime))));
                    //return String.Format("TO_DATE('{0}','{1}')", dateTime.ToString("yyyy-M-dd"), "yyyy-MM-dd");
                }
                else if (DigitHelper.IsDigit(val.ToString()))
                {
                    return string.Format("'{0}'",val.ToString());
                    //return val;
                }
                else if ((val is String))
                {
                    return String.Format("'{0}'", val.ToString());
                }
                else if (val == System.DBNull.Value)
                {
                    return "''";
                }
                return val;
            }
            private set { val = value; }
        }


        /// <summary>
        /// Casts the cell's value to the generic argument type
        /// </summary>
        /// <typeparam name="T">Object type to convert to</typeparam>
        public T Cast<T>()
        {
            return (Value == null || Value is DBNull) ?
                default(T) :
                (T)Convert.ChangeType(Value, typeof(T));
        }

        /// <summary>
        /// Cell's value as a string
        /// </summary>
        public override string ToString()
        {
            return Value.ToString();
        }
    }
}
